/*
************************************************************************************************************************
	Creates estimation dataset $workdata/cohort with information on:
	- Social assistance (sa)

	Uses estimation dataset:
	- $workdata/baseline (see "$data_do/baseline")

	Uses auxiliary datasets:
	- $temp/bef_red2_`year' (see "$data_do/bef_red")
	- $temp/mia_con_`year'  (see "$data_do/mia_con")

	Registers: Variables (Years) 
	- DREAM: pnr, y_01-y_52(53) (1996-2010)
************************************************************************************************************************
*/

***************************************************************
* start with baseline sample.
***************************************************************

use "$workdata/baseline", clear /* see $data_do/baseline.do */

***************************************************************
* collect observations complying with selection criteria
* half a year on both sides of cutoff.
* Save in dataset $temp/cohortpop
***************************************************************
forvalues c=24/25   {   
preserve
tempfile age_floor`c'
g keep=0
replace keep=1 if `c'==24 & age_q>=24.5 & age_q<25
replace keep=1 if `c'==25 & age_q>=25 & age_q<25.5
keep if keep==1
collapse e, by(pnr)
g age_floor`c'=`c'
save `age_floor`c''
restore
}

use `age_floor24', clear
merge 1:1 pnr using `age_floor25', keep(match)
capture drop _merge
save  $temp/cohortpop, replace

***************************************************************
* Setting start dates for each year
***************************************************************

forvalues year = 1996/2010 {

if `year'==1996 local day1=mdy(1,1,1996)
if `year'==1997 local day1=mdy(12,30,1996)
if `year'==1998 local day1=mdy(12,29,1997)
if `year'==1999 local day1=mdy(1,4,1999)
if `year'==2000 local day1=mdy(1,3,2000)
if `year'==2001 local day1=mdy(1,1,2001)
if `year'==2002 local day1=mdy(12,31,2001)
if `year'==2003 local day1=mdy(12,30,2002)
if `year'==2004 local day1=mdy(12,29,2003)
if `year'==2005 local day1=mdy(1,3,2005)
if `year'==2006 local day1=mdy(1,2,2006)
if `year'==2007 local day1=mdy(1,1,2007)
if `year'==2008 local day1=mdy(12,31,2007)
if `year'==2009 local day1=mdy(12,29,2008)
if `year'==2010 local day1=mdy(1,4,2010)


***************************************************************
* merge data with data on public income transfers y_1-y_52(y_53).
***************************************************************

capture drop _merge
merge m:1 pnr using "$rawdata/dream`year'", keep(master match)
preserve
keep y_*
local num_weeks=c(k)
restore
display `num_weeks'
forvalues yvar= 1/9 {
rename y_0`yvar' y_`yvar'
}

***************************************************************
* Calculate weekly age
***************************************************************
forvalues dream_week = 1/`num_weeks' {
g age_`dream_week'= ((`day1'+(7*(`dream_week'))-foed_dag))/365.25
}

forvalues week = 1/`num_weeks'  {
g month_`week'=0
}

forvalues month = 1/12 {
forvalues week = 1/`num_weeks'  {

if `month'<12 ///
&  age_`week'>= ((mdy(`month',1,`year') - foed_dag)/365.25) ///
&  age_`week'<  ((mdy(`month'+1,1,`year') - foed_dag)/365.25) {
replace month_`week'=`month'
}

if `month'==12 ///
&  age_`week'>= ((mdy(`month',1,`year') - foed_dag)/365.25) ///
&  age_`week'<=  ((mdy(12,31,`year') - foed_dag)/365.25) {
replace month_`week'=`month'
}
}
}

***************************************************************
* Create weekly observations of public income transfer (y) in weeks 1-`num_weeks'.
* (wide to long)
* Save cohort_3_`year'
***************************************************************

expand `num_weeks'
sort pnr 
capture drop week
gen week=1
bysort pnr: replace week=sum(week)
capture drop age
gen age=0
capture drop month
gen month=0
capture drop y
gen y=0

forvalues week=1/`num_weeks' {
bysort pnr: replace age=age_`week' if week==`week'
bysort pnr: replace y=y_`week' if week==`week'
bysort pnr: replace month=month_`week' if week==`week'
}
 
drop y_* age_* month_*   _merge
save "$temp/cohort_3_`year'", replace
}

***************************************************
*Collect yearly dataset to one.
*Save cohort_3_`year'
***************************************************


use pnr foed_dag age week month year y  koen edul eduf d_childl d_childf ie_type fm_markl fm_markf civstl civstf using "$temp/cohort_3_1996", clear
forvalues year=1997/2010 {
append using "$temp/cohort_3_`year'", keep(pnr foed_dag age week month year y koen edul eduf  d_childl d_childf ie_type fm_markl fm_markf  civstl civstf)
}

********************************************
* Defining social assistance (sa)
* Define age in months
* Save cohort
********************************************

capture drop sa
g sa=0
replace sa=1 if y>=130 & y<=139 ///
              | y>=140 & y<=149 ///
			  | y>=700 & y<=709 ///
			  | y>=710 & y<=719 ///
			  | y>=720 & y<=729 ///
			  | y>=730 & y<=739 /*social assistance*/

replace month=12 if month==0
capture drop age_q
g age_q=floor( (age-floor(age))*12)*(1/12) + floor(age)
g b_month=month(foed_dag)
g b_year=year(foed_dag)

save "$workdata/cohort", replace